﻿using UnityEngine;

namespace UnityMovementAI
{
    /// <summary>
    /// only walk towards the target, rember to set the target.
    /// will be able sensor the target even behind the wall.
    /// </summary>
    public class SeekUnit : MonoBehaviour
    {
        /// <summary>
        /// the target to seek
        /// </summary>
        public Transform target;
        /// <summary>
        /// leave a distance to the target
        /// </summary>
        public float fKeepDistance = 2f;

        SteeringBasics steeringBasics;

        [Header("运行时")]
        public bool bIsActive = false;
        public bool bStopMove= false;

        void Start()
        {
            steeringBasics = GetComponent<SteeringBasics>();
        }

        void FixedUpdate()
        {
            if (!bIsActive) return;
            Vector3 accel;
            if (Vector3.Distance(transform.position, target.position) > fKeepDistance){
               accel = steeringBasics.Seek(target.position);
            }else{
                accel = steeringBasics.Around(target.position, fKeepDistance/2, target.GetComponent<MovementAIRigidbody>());
            }

            if (!bStopMove) steeringBasics.Steer(accel);
            else steeringBasics.Stand();
            steeringBasics.LookTargetPosition(target.position);
        }

        public void SetActive(bool bActive){
            bIsActive = bActive;
        }
        public void SetStopMove(bool bStopMove){
            this.bStopMove = bStopMove;
        }
    }
}